package com.lanqiao.interceptor;

import com.lanqiao.domain.UsersInfo;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 用户是否登录的安全性检查
 *
 * @author 张建平 on 2020/8/14.
 */
public class SecureInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        HttpSession session = request.getSession();
        // 从会话中获得登录的用户信息，如果未登录则为null
        UsersInfo logUsersInfo = (UsersInfo) session.getAttribute("logUsersInfo");
        if (logUsersInfo == null) {
            // 重新登录
            response.sendRedirect(request.getContextPath());
            return false;
        }

        return true;
    }
}
